package A3GCPackage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;

public class BasicRankingScheme 
	implements RankingScheme
{
	public BasicRankingScheme()
	{
		
	}

	@Override
	public List<SingleNodeData> rank(NodeData data, String group) 
	{
		// local data structures
		PriorityQueue<SingleNodeData> pq_samegroup = new PriorityQueue<SingleNodeData>();
		PriorityQueue<SingleNodeData> pq_freegroup = new PriorityQueue<SingleNodeData>();
		PriorityQueue<SingleNodeData> pq_remainder = new PriorityQueue<SingleNodeData>();
		
		// get the nodes from NodeData
		Collection<SingleNodeData> col = data.getNodes();
		
		// iterator through the nodes placing into groups
		Iterator<SingleNodeData> iter = col.iterator();
		
		while ( iter.hasNext() )
		{
			SingleNodeData snd = iter.next();
			
			// if active not unusable
			if ( !snd.active )
			{
				// if groupname == group ??
				if ( snd.group.compareTo(group) == 0 )
				{
					pq_samegroup.add( snd );
				}
				// if not in a group
				else if ( snd.group.compareTo("") == 0 )
				{
					pq_freegroup.add( snd );
				}
				// else sort by timestamp
				else
				{
					pq_remainder.add( snd );
				}
			}
		}
		
		// merge into a single list
		List<SingleNodeData> output = new ArrayList<SingleNodeData>();
		
		output.addAll( pq_samegroup );
		output.addAll( pq_freegroup );
		output.addAll( pq_remainder );
		
		return output;
	}
	
}
